<?php
//用户
namespace app\siteadmin\model;

use think\facade\Db;
use think\Model;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

class User extends Model{
    protected $pk = 'uid';
    // 模型初始化
    protected static function init()
    {
        parent::init();
        //TODO:初始化内容
    }


    public static function info($where){
        $find = self::where($where)->field('*')->find();
        return $find;
    }

    //添加用户
    public static function add_user($data){
        $in = self::insertGetId($data);
        return $in;
    }


    //登录验证
    public static function find_info($username,$password){
        $info = self::where('username','=',$username)
            ->where('password','=',$password)
            ->find();
        return $info;
    }


    //修改密码
    public static function changePassword($uid,$password){
        $in = self::where(['uid' => $uid])->update(['password' => $password]);
        return $in;
    }


    //最后登录时间
    public static function lasttime($uid){
        $in = self::update(['lasttime' => time()], ['uid' => $uid]);
        return $in;
    }

    //管理员添加
    public static function userAdd(array $data){
        return self::create($data);
    }

    //管理员变更
    public static function userEdit($uid,$update){
        $in = self::where(['uid' => $uid])->update($update);
        return $in;
    }

    public static function userSelect($where){
        return self::where($where)->select();
    }

    public static function datalist($where,$page,$num){
        return self::where($where)->page($page,$num)->select();
    }

    public static  function listCount($where){
        return self::where($where)->field("id")->count();
    }

    public function listUser($page,$num,$where){
        $list = $this::name('user')
            ->field('*')
            ->where($where)
            ->order('uid', 'desc')
            ->limit(($page-1)*$num,$num)
            ->select();
        return $list;
    }

    public function countUser($where) {
        return  $this::name('user')
            ->where($where)
            ->count();
    }

    public function listAdminUser($page,$num,$where,$whereNull=''){
        if(!empty($whereNull)){
            $list = $this::name('user')
                ->field("`u`.`uid`,`u`.`username`,`u`.`addtime`,`u`.`lasttime`,`u`.`enable`,`u`.`remarks`,r.name as rolename")
                ->alias('u')

                ->leftJoin("role_user ru","u.uid = ru.uid")
                ->leftJoin("role r","r.id = ru.rid")
                ->where($where)
                ->whereNull($whereNull)
                ->select();
        }
        else{
            $list = $this::name('user')
                ->field("`u`.`uid`,`u`.`username`,`u`.`addtime`,`u`.`lasttime`,`u`.`enable`,`u`.`remarks`,r.name as rolename")
                ->alias('u')
                ->leftJoin("role_user ru","u.uid = ru.uid")
                ->leftJoin("role r","r.id = ru.rid")
                ->where($where)
                ->select();
        }


        return $list;
    }

    public function countAdminUser($where,$whereNull='') {
        if(!empty($whereNull)){
            return $list = $this::name('user')
                ->alias('u')
                ->leftJoin("role_user ru","u.uid = ru.uid")
                ->leftJoin("role r","r.id = ru.rid")
                ->where($where)
                ->whereNull($whereNull)
                ->count();
        }
        else
        {
            return $list = $this::name('user')
                ->alias('u')

                ->leftJoin("role_user ru","u.uid = ru.uid")
                ->leftJoin("role r","r.id = ru.rid")
                ->where($where)

                ->count();
        }



    }
}
